﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PrinteMaster
{
    public partial class 增加会员窗口 : Form
    {
        private string m_lpstrQQHint, m_lpstrPWHint, m_lpstrNameHint, m_lpstrValueHint, m_lpstrPhoneNumberHint, m_lpstrPhoneNumberShortHint;
        private MySqlHelper.ManagerInfo m_ManagerInfo;
        public 增加会员窗口()
        {
            InitializeComponent();

        }

        private void 增加会员窗口_Load(object sender, EventArgs e)
        {
            textBoxQQ号.Text = m_lpstrQQHint;
            textBox短手机号.Text = m_lpstrPhoneNumberShortHint;
            textBox密码.Text = m_lpstrPWHint;
            textBox手机号.Text = m_lpstrPhoneNumberHint;
            textBox首充金额.Text = m_lpstrValueHint;
            textBox姓名.Text = m_lpstrNameHint;
        }

        public static 增加会员窗口 create(MySqlHelper.ManagerInfo info,客服窗口 window)
        {
            增加会员窗口 c = new 增加会员窗口();
            c.m_ManagerInfo = info;
            c.ShowDialog(window);
            return c;
        }
        #region 事件映射
        private void textBox1_Leave(object sender, EventArgs e)
        {
            //Function.inputInt(sender, e);
            Function.inputText(textBoxQQ号,m_lpstrQQHint);
        }

        private void textBox密码_Leave(object sender, EventArgs e)
        {
            Function.inputText(textBoxQQ号, m_lpstrQQHint);
        }

        private void textBox密码_Enter(object sender, EventArgs e)
        {
            Function.inputText(textBox密码, m_lpstrPWHint);
        }

        private void textBoxQQ号_Enter(object sender, EventArgs e)
        {
            Function.inputText(textBox密码, m_lpstrPWHint);
        }

        private void textBox姓名_Enter(object sender, EventArgs e)
        {
            Function.inputText(textBox姓名, m_lpstrNameHint);
        }

        private void textBox姓名_Leave(object sender, EventArgs e)
        {
            Function.inputText(textBox姓名, m_lpstrNameHint);
        }

        private void textBox首充金额_Enter(object sender, EventArgs e)
        {
            Function.inputText(textBox首充金额, m_lpstrValueHint);
        }

        private void textBox首充金额_Leave(object sender, EventArgs e)
        {
            Function.inputText(textBox首充金额, m_lpstrValueHint);
        }

        private void textBox手机号_Enter(object sender, EventArgs e)
        {
            Function.inputText(textBox手机号, m_lpstrPhoneNumberHint);
        }

        private void textBox手机号_Leave(object sender, EventArgs e)
        {
            Function.inputText(textBox手机号, m_lpstrPhoneNumberHint);
        }

        private void textBox短手机号_Enter(object sender, EventArgs e)
        {
            Function.inputText(textBox短手机号, m_lpstrPhoneNumberShortHint);
        }

        private void textBox短手机号_Leave(object sender, EventArgs e)
        {
            Function.inputText(textBox短手机号, m_lpstrPhoneNumberShortHint);
        }

        private void textBox短手机号_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Function.isPassWordValue();
            Function.inputInt(sender, e);
        }

        private void textBoxQQ号_KeyPress(object sender, KeyPressEventArgs e)
        {
            Function.inputInt(sender, e);
        }

        private void textBox首充金额_KeyPress(object sender, KeyPressEventArgs e)
        {
            Function.inputInt(sender, e);
        }

        private void textBox手机号_KeyPress(object sender, KeyPressEventArgs e)
        {
            Function.inputInt(sender, e);
        }
        #endregion
        private void button1_Click(object sender, EventArgs e)
        {
            //验证有效性
            if (textBoxQQ号.Text.Length == 0 || textBoxQQ号.Text.Length > 20)
            {
                MessageBox.Show("qq号不能为空!且长度必须小于21");
                return;
            }
            if (textBox手机号.Text.Length != 11)
            {
                MessageBox.Show("手机长号必须为11位!");
                return;
            }
            if (textBox姓名.Text.Length == 0 || textBox姓名.Text.Length >10)
            {
                MessageBox.Show("姓名不能为空!且长度必须小于6个汉字!");
                return;
            }
            if (textBox密码.Text.Length == 0 )
            {
                textBox密码.Text = textBoxQQ号.Text;
            }
            if (textBox首充金额.Text.Length == 0)
            {
                textBox首充金额.Text = "0";
            }
            if (textBox短手机号.Text != "" && textBox短手机号.Text.Length != 6)
            {
                MessageBox.Show("手机短号必须为6位!");
                return;
            }
            //如果没退出，填充结构体。
            DataBase.UserInfo info = new DataBase.UserInfo();
            info.id = textBoxQQ号.Text;
            info.money = 0.0;// Convert.ToDouble(textBox首充金额.Text);
            info.name = textBox姓名.Text;
            info.phoneNumber = textBox手机号.Text;
            info.pw = textBox密码.Text;
            info.shortPhoneNumber = textBox短手机号.Text;
            DataBase.Error error = DataBase.getInstance().addUser(info);
            if (error == DataBase.Error.ok)
            {
                MessageBox.Show("注册会员成功!提示语句已复制到剪贴板，你也可以在下方寻找到它.");
                var hint = string.Format("自助打印注册成功！！\r\n账号：{1}\r\n姓名：{2}\r\n密码同账号，请尽快修改并且充值",
                    m_ManagerInfo.id, textBoxQQ号.Text, textBox姓名.Text, textBox手机号.Text);
                textBox提示语句.Text = hint;
                Clipboard.SetDataObject(textBox提示语句.Text);
                //this.Close();
                //return;
            }
            else if (error == DataBase.Error.已有该账号)
            {
                MessageBox.Show("QQ号或手机号重复,不能注册");
                textBox密码.Text = "";
            }
            else
            {
                MessageBox.Show("注册会员失败，失败原因=>" + error.ToString());
                textBox密码.Text = "";
            }
            ///////为日志添加记录
            //Log.managerLog("登陆客户端", info.id, info.name, info.user);
            var log = string.Format("为{0}注册账号[QQ:{1},手机号:{2}", info.name, info.id, info.phoneNumber);
            Log.managerLog("为" + info.name + "注册账号", 主窗口.m_MangerInfo.id, 主窗口.m_MangerInfo.name, 主窗口.m_MangerInfo.user);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox提示语句.Text.Length != 0)
            {
                MessageBox.Show("复制语句成功，你可以在qq窗口内按下ctrl+v或鼠标右键点击复制!");
                Clipboard.SetDataObject(textBox提示语句.Text);
            }
            else
            {
                MessageBox.Show("没有可复制的语句!");
            }
        }

        private void textBox姓名_TextChanged(object sender, EventArgs e)
        {
            var textbox = (TextBox)sender;
            var temp = textbox.Text.Split('/');
            if (temp.Length == 1)
                return;
            if (temp.Length != 3)
            {
                MessageBox.Show("非法的参数，请仔细检查字符串中的斜杠数量");
                return;
            }
            textBox姓名.Text = temp[0];
            textBox手机号.Text = temp[1];
            textBox短手机号.Text = temp[2];
            
        }

        private void textBox手机号_TextChanged(object sender, EventArgs e)
        {

        }

    }
}
